//
//  TestView.m
//  AutoTest
//
//  Created by Viking Lai on 2018/5/25.
//  Copyright © 2018年 Viking Lai. All rights reserved.
//

#import "TestView.h"
#import "MACRO.h"

@interface TestView ()

@end

@implementation TestView
@synthesize dataSource = _arrayDataSource;
- (NSArray *)dataSource
{
    return _arrayDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

#pragma mark - init
- (instancetype)initWithScriptName:(NSString *)name
{
    self = [super initWithNibName:@"TestView" bundle:nil];
    
    if (self) {
        _arrayDataSource = [[NSMutableArray alloc] init];

        [self loadScriptToTestView:name];
    }
    
    return self;
}

- (void)loadScriptToTestView:(NSString *)path
{
    NSInteger row = 1, subRow = 1,itemCount = 1;
    NSArray *scriptItems = [NSArray arrayWithContentsOfFile:path];
    
    for (NSDictionary *dicItem in scriptItems) {
        NSString *mainItem = [dicItem objectForKey:@"ItemName"];
        NSString *mainItemResult = @"";
        
        NSString *idNumber = [NSString stringWithFormat:@"%ld", row];
        
        NSMutableDictionary * dicMainItem = [[NSMutableDictionary alloc] init];
        NSMutableArray *arraySubItems = [[NSMutableArray alloc] init];
        
        BOOL isShowValue = [[dicItem objectForKey:@"ShowValue"] boolValue];
        
        if (1) {
            NSMutableDictionary* dicRowItem = [[NSMutableDictionary alloc] init];
            
            NSString* idNumber = [NSString stringWithFormat:@"%03ld",itemCount++];
            
            NSString* itemName = [dicItem objectForKey:@"ItemName"];
            NSString* lower = [dicItem objectForKey:@"LowerLimit"];
            NSString* upper = [dicItem objectForKey:@"UpperLimit"];
            NSString* unit = [dicItem objectForKey:@"Unit"];
            
            [dicRowItem setObject:idNumber forKey:@"ID"];
            [dicRowItem setObject:itemName forKey:@"ITEM"];
            [dicRowItem setObject:lower == nil ? @"" :lower forKey:@"LowerLimit"];
            [dicRowItem setObject:upper == nil ? @"" :upper forKey:@"UpperLimit"];
            [dicRowItem setObject:unit == nil ? @"" :unit forKey:@"Unit"];
            [dicRowItem setObject:@"" forKey:@"TestResult"];
            [dicRowItem setObject:[dicItem objectForKey:@"ShowValue"] forKey:@"ShowValue"];
            [dicRowItem setObject:@(NO) forKey:@"ResultFlag"];
            [dicRowItem setObject:@(YES) forKey:@"CHECK"];
            [dicRowItem setObject:[dicItem objectForKey:@"isUpLoad"] == nil ? @(NO):[dicItem objectForKey:@"isUpLoad"] forKey:@"isUpLoad"];
            [arraySubItems addObject:dicRowItem];
            
        }
        
        [dicMainItem setObject:idNumber forKey:@"ID"];
        [dicMainItem setObject:mainItem forKey:@"ITEM"];
        [dicMainItem setObject:mainItemResult forKey:@"MainItemResult"];
        [dicMainItem setObject:arraySubItems forKey:@"SubItem"];
        [dicMainItem setObject:@([[dicItem objectForKey:@"isUpLoad"] boolValue]) forKey:@"isUpLoad"];
        [dicMainItem setObject:@(YES) forKey:@"CHECK"];
        
        [_arrayDataSource addObject:dicMainItem];
        row++, subRow = 1;
        
    }
    
}
#pragma mark - outline protocols
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if (item == nil)
        return _arrayDataSource.count;
    
    if ([item isKindOfClass:[NSDictionary class]]) {
        return [[item objectForKey:SUBITEM] count];
    }
    
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)child ofItem:(id)item
{
    if (item == nil) {
        return [_arrayDataSource objectAtIndex:child];
    }
    
    if ([item isKindOfClass:[NSDictionary class]]) {
        return [[item objectForKey:SUBITEM] objectAtIndex:child];
    }
    
    return nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if ([item objectForKey:SUBITEM] != nil)
        return YES;
    else
        return NO;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    id objectValue = nil;
    
    //    if ([[tableColumn identifier] isEqualToString:@"CHECK"]) {
    //        objectValue = [NSNumber numberWithBool:[[item objectForKey:@"CHECK"] boolValue]];
    //    } else
    {
        objectValue = [item objectForKey:[tableColumn identifier]];
    }
    
    return objectValue;
}


@end
